package cn.kgc.systemservice.service;


import cn.kgc.bean.House;
import cn.kgc.bean.System;
import cn.kgc.myhousemapper.mapper.HouseMapper;
import cn.kgc.myhousemapper.mapper.SystemMapper;
import cn.kgc.service.SystemService;
import cn.kgc.utils.CrowdFundingUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Service
@Component
public class SystemServiceImpl implements SystemService {

    @Resource
    SystemMapper systemMapper;
    @Resource
    HouseMapper houseMapper;

    @Override
    public System login(String username, String password) {
        System system=null;
/*        SystemExample systemExample=new SystemExample();
        SystemExample.Criteria criteria =systemExample.createCriteria();
        criteria.andUsernameEqualTo(username);*/
        List<System> systems = systemMapper.selectList(new QueryWrapper<System>().eq("username",username));
        if (CrowdFundingUtils.collectionEffective(systems)){
                system=systems.get(0);
            String md5 = CrowdFundingUtils.md5(password);
            if (md5.equals(system.getPassword())){
                //成功
                return system;
            }else {
                system=null;
            }
        }
        return system;
    }

    @Override
    public PageInfo<House> selectAll(Integer pageNum, String keyword) {
        //设置当前页
        PageHelper.startPage(pageNum,3 );
        PageHelper.orderBy("id desc");
        if (CrowdFundingUtils.stringEffective(keyword)){
        /*    HouseExample example=new HouseExample();
            HouseExample.Criteria criteria=example.createCriteria();*/
/*            criteria.andFrnameLike("%"+keyword+"%");
            HouseExample.Criteria criteria1=example.createCriteria();
            criteria1.andHouseaddrLike("%"+keyword+"%");
            example.or(criteria1);*/

            List<House> houseList = houseMapper.selectList(new QueryWrapper<House>().like("frname",keyword).or().like("houseaddr",keyword));
            PageInfo<House> pageInfo=new PageInfo<>(houseList);
            return pageInfo;
        }else{
            List<House> list = houseMapper.selectList(null);
            PageInfo<House> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }
    }

    @Override
    public Integer saveHouse(House house) {
        return houseMapper.insert(house);
    }

    @Override
    public House selectAllById(Integer id) {
        return houseMapper.selectById(id);
    }

    @Override
    public Integer updateHouse(House house) {
        return houseMapper.updateById(house);
    }

    @Override
    public Integer delHouse(Integer id) {
        return  houseMapper.deleteById(id);
    }
}
